package com.atguigu.flink.day03;

import com.atguigu.flink.beans.WaterSensor;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author Felix
 * @date 2023/12/2
 * 该案例演示了keyby操作以及聚合
 *      keyby作用：是对数据进行分组(重分区)
 *      keyby不算一个转换算子，只是一个分组操作
 *      flink提供了一些聚合算子sum/min/max/minBy/maxBy/reduce，但是这些算子在使用前，必须先进行keyby分组
 */
public class Flink11_keyby {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        DataStreamSource<WaterSensor> wsDS = env.fromElements(
            new WaterSensor("sensor_1", 1L, 10),
            new WaterSensor("sensor_1", 2L, 30),
            new WaterSensor("sensor_1", 3L, 20),
            new WaterSensor("sensor_1", 4L, 40)
        );

        KeyedStream<WaterSensor, String> keyedDS = wsDS
            .keyBy(WaterSensor::getId);


        // keyedDS.print();

        // keyedDS.max("vc").print();
        SingleOutputStreamOperator<WaterSensor> sumDS = keyedDS.maxBy("vc");
        sumDS.print();


        env.execute();
    }
}
